package mt.tests;

import mt.Order;
import mt.comm.ServerSideMessage;
import mt.dummy.MicroTraderServerDummy;
import mt.dummy.ServerCommDummy;

import org.junit.Test;

public class MicroTraderServerDummyTest {

	
	
	@Test
	public void testRun() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
	}


	@Test
	public void testPlaceNewOrder() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		Order order1 = Order.createBuyOrder("Jase2", "Stock2", 1, 2);
		Order order2 = Order.createSellOrder("Jase", "Stock", 100, 2);
		mtsd.placeNewOrder(order1);
		mtsd.placeNewOrder(order);
		mtsd.placeNewOrder(order2);
		mtsd.placeNewOrder(order);
		mtsd.placeNewOrder(order1);

	}

	@Test
	public void testFindMatchingOrder() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		Order order1 = Order.createBuyOrder("Jase2", "Stock2", 1, 2);
		Order order2 = Order.createSellOrder("Jase", "Stock", 20, 200);
		Order order3 = Order.createSellOrder("Jase3", "Stock", 1, 2);
		mtsd.addNewOrder(order);
		mtsd.addNewOrder(order1);
		mtsd.addNewOrder(order2);
		mtsd.addNewOrder(order3);
		mtsd.findMatchingOrder(order);
		mtsd.findMatchingOrder(order2);
		mtsd.findMatchingOrder(order3);
	}

	@Test
	public void testAddNewOrder() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		mtsd.addNewOrder(order);
		
		
	}

	@Test
	public void testStart() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
	}

	@Test
	public void testUpdateClient() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		mtsd.placeNewOrder(order);
		mtsd.updateClient("Jase");
		
	}
		
	@Test
	public void testUpdateAllClients() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		final Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		final Order order2 = Order.createBuyOrder("Jase2", "Stock2", 10, 20);
		final Order order3 = Order.createBuyOrder("Jase3", "Stock3", 100, 200);
		
		ServerSideMessage ssm = new ServerSideMessage() {
			
			@Override
			public Type getType() {
				return Type.CONNECTED;
			}
			
			@Override
			public String getSenderNickname() {
				return "Jase";
			}
			
			@Override
			public Order getOrder() {
				return order;
			}
		};
		ServerSideMessage ssm2 = new ServerSideMessage() {
			
			@Override
			public Type getType() {
				return Type.DISCONNECTED;
			}
			
			@Override
			public String getSenderNickname() {
				return "Jase2";
			}
			
			@Override
			public Order getOrder() {
				return order2;
			}
		};
		ServerSideMessage ssm3 = new ServerSideMessage() {
			
			@Override
			public Type getType() {
				return Type.NEW_ORDER;
			}
			
			@Override
			public String getSenderNickname() {
				return "Jase3";
			}
			
			@Override
			public Order getOrder() {
				return order3;
			}
		};
		mtsd.handleMessage(ssm);
		mtsd.handleMessage(ssm2);
		mtsd.handleMessage(ssm3);
		mtsd.updateAllClients();
		}

	@Test
	public void testUpdateAllClientsOrder() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		
		final Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		final Order order2 = Order.createBuyOrder("Jase2", "Stock2", 10, 20);
		final Order order3 = Order.createBuyOrder("Jase3", "Stock3", 100, 200);
		
		ServerSideMessage ssm = new ServerSideMessage() {
			
			@Override
			public Type getType() {
				return Type.CONNECTED;
			}
			
			@Override
			public String getSenderNickname() {
				return "Jase";
			}
			
			@Override
			public Order getOrder() {
				return order;
			}
		};
		ServerSideMessage ssm2 = new ServerSideMessage() {
			
			@Override
			public Type getType() {
				return Type.DISCONNECTED;
			}
			
			@Override
			public String getSenderNickname() {
				return "Jase2";
			}
			
			@Override
			public Order getOrder() {
				return order2;
			}
		};
		ServerSideMessage ssm3 = new ServerSideMessage() {
			
			@Override
			public Type getType() {
				return Type.NEW_ORDER;
			}
			
			@Override
			public String getSenderNickname() {
				return "Jase3";
			}
			
			@Override
			public Order getOrder() {
				return order3;
			}
		};

		mtsd.handleMessage(ssm);
		mtsd.handleMessage(ssm2);
		mtsd.handleMessage(ssm3);
		mtsd.updateAllClients(order);
		
	}

	@Test
	public void testDisconnectClient() {
		MicroTraderServerDummy mtsd = new MicroTraderServerDummy();
		ServerCommDummy scd = new ServerCommDummy();
		mtsd.start(scd);
		Order order = Order.createBuyOrder("Jase", "Stock", 1, 2);
		Order order1 = Order.createBuyOrder("Jase2", "Stock2", 1, 2);
		Order order2 = Order.createSellOrder("Jase", "Stock", 20, 200);
		mtsd.addNewOrder(order);
		mtsd.addNewOrder(order1);
		mtsd.addNewOrder(order2);
		mtsd.disconnectClient("Jase");
	}

}
